﻿using System;
using System.Collections.Generic;
using Weather.UA.Model;

namespace Weather.UA
{
    public partial class WeatherUA
    {
        private readonly Sector _sector;
        private readonly List<Sector> _sectors;

        public Sector GetSectorById(int id)
        {
            const string apiUrl = "{0}/{1}/country/?lang={2}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, Language);
            var document = _loadXDocument(url);
            var sectors = document.Element("country").Descendants("region");
            foreach (var sector in sectors)
            {
                var attributeId = sector.Attribute("id");
                if (attributeId != null && Convert.ToInt32(attributeId.Value) == id)
                {
                    _sector.Id = id;
                    _sector.Name = sector.Value;
                }
            }
            return _sector;
        }
        public List<Sector> GetSectorsByName(string name)
        {
            const string apiUrl = "{0}/{1}/country/?lang={2}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, Language);
            var document = _loadXDocument(url);
            var sectors = document.Element("country").Descendants("region");
            foreach (var sector in sectors)
            {
                var attributeId = sector.Attribute("id");
                if (attributeId != null & sector.Value.Contains(name))
                {
                    var s = new Sector();
                    s.Id = Convert.ToInt32(attributeId.Value);
                    s.Name = sector.Value;
                    _sectors.Add(s);
                }
            }
            return _sectors;
        }
    }
}
